package wowmobile;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class WOWMobile
{

    private boolean conrunning;
    private Server server;
    private ExecutorService exec;

    WOWMobile() {
        server= new Server();
        exec= Executors.newCachedThreadPool();
    }

    private void mainCommandsPrompt(String command[]) {
        if (command.length == 1) {
            if (command[0].toLowerCase().equals("help")) {
                System.out.println("help - does this... Type 'help command' to see what 'command' does");
                System.out.println("start - starts server");
                System.out.println("stop - stops server");
                System.out.println("restart - restarts server");
                System.out.println("quit - closes this program and server");
            } else if (command[0].toLowerCase().equals("start")) {
                System.out.println("Server will start.");
                exec.execute(server);
            } else if (command[0].toLowerCase().equals("stop")) {
                System.out.println("Server will stop.");
                server.killServer();
            } else if (command[0].toLowerCase().equals("restart")) {
                System.out.println("Server will restart.");
                server.killServer();
                exec.execute(server);
            } else if (command[0].toLowerCase().equals("quit")) {
                System.out.println("Program will quit.");
                server.killServer();
                this.conrunning = false;
            } else if (command[0].toLowerCase().equals("save")) {
                System.out.println("Something");
            } else if (command[0].toLowerCase().equals("info")) {
                System.out.println("Something");
            } else if (command[0].toLowerCase().equals("add")) {
                System.out.println("Something");
            } else if (command[0].toLowerCase().equals("msg")) {
                System.out.println("Something");
            } else if (command[0].toLowerCase().equals("rebuild")) {
                System.out.println("Database will be rebuilt");
                Database.RebuildDatabase();
            } else {
                System.out.println("No such command found. Type 'help', to see full commands list.");
            }
        } else if (command.length > 1) {
            if (command[0].toLowerCase().equals("help") && command.length == 2) {
                if (command[1].toLowerCase().equals("help")) {
                    System.out.println("help - does this... Type 'help command' to see what 'command' does");
                } else if (command[1].toLowerCase().equals("start")) {
                    System.out.println("start - starts server");
                } else if (command[1].toLowerCase().equals("stop")) {
                    System.out.println("stop - stops server");
                } else if (command[1].toLowerCase().equals("restart")) {
                    System.out.println("restart - restarts server");
                } else if (command[1].toLowerCase().equals("quit")) {
                    System.out.println("quit - closes this program and server");
                } else {
                    System.out.println("No such command found. Type 'help', to see full commands list.");
                }
            } else {
                System.out.println("Too many arguments. Type 'help', to see full commands list.");
            }
        } else {
            System.out.print("Ups. Console input bug. Report it asap: in command.length > 1 test");
        }
    }

    private void consolePrompt() {
        System.out.println("Welcome to WOWMobile server console. Type 'help', to see full commands list.");

        conrunning = true;
        InputStreamReader input;
        BufferedReader reader;
        input = new InputStreamReader(System.in);
        reader = new BufferedReader(input);
        while (conrunning) {
            

            try {
                String inputStr = reader.readLine();
                String command[] = inputStr.split(" ");
                mainCommandsPrompt(command);
            } catch (Exception e) {
                System.out.print("Ups. Console input bug. Report this asap: "+e);
            }
        }
        System.out.println("Program closed");
        System.exit(0);
    }

    public static void main(String[] args) {
        WOWMobile w=new WOWMobile();
        w.consolePrompt();
    }
}
